<?php
//ver=10182
class HTMLDocument{	
	
	//Template constant.
	const TPL='<html><head><meta><link rel="icon" type="image/ico"><title></title></head><body></body></html>';
	
	//DOCTYPE Constants.
	const DT4='<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">';
	const DT5='<!DOCTYPE html>';
	
	//HTML Version. May be 4 or 5.
	private $html_ver=5;	
	//The DOCTYPE of the HTML document.Must be a String.
	private $doctype;
	//Language.Must be a String.
	public $lang='zh-cn';
	//Character Set
	private $charset;
	//Page Icon
	private $icon;
	//Document Title
	private $title;
	
	//The <html> element of HTML document.
	public $root;
	public $head;
	public $body;	
	
	//$template must be an html string.
	public function __construct($html_ver=5){
		if(is_int($html_ver))$this->html_ver=$html_ver;
		//DOCTYPE
		$this->setDoctype($html_ver);
		//Character Set
		$this->setCharset('utf-8');
		//Icon
		$this->setIcon('Metro/favicon');
		//Title
		$this->setTitle('Ceibit');
		//Head
		$this->head=new HTMLElement('head');
		//Body
		$this->body=new HTMLElement('body');
		//Root
		$root=new HTMLElement('html');
		$root->addAtt('lang',$this->lang);
		$this->root=$root;
	}
	
	//Set the Document's DOCTYPE
	public function setDoctype($dt_ver=5){
		if($dt_ver==5)
			$this->doctype=self::DT5;
		if($dt_ver==4)
			$this->doctype=self::DT4;	
	}
	
	//Set the Document's Charset
	public function setCharset($char='utf-8'){
		if(!empty($char)){
			$meta=new HTMLElement('meta');
			if($this->html_ver==5)
				$meta->addAtt('charset',$char);
			if($this->html_ver==4){
				$meta->addAtt('http-equiv','Content-Type');
				$meta->addAtt('content',"text/html; charset=$char");
			}		
			$this->charset=$meta;
		}			
	}
	
	//Set the Document's Icon
	public function setIcon($icon_url='Metro/favicon'){
		if(!empty($icon_url)){
			$link=new HTMLElement('link');
			$link->addAtt('rel','icon');
			$link->addAtt('type','image/ico');
			$link->addAtt('href',"../theme/$icon_url.ico");
			$this->icon=$link;
		}			
	}
	
	//Set the Document's Title
	public function setTitle($title_text=''){
		if(is_string($title_text)){
			$title=new HTMLElement('title');
			$title->addChild(trim($title_text));
			$this->title=$title;
		}			
	}
	
	//Load a new template.
	public function reload($template,$is_file=TRUE){
		if($if_file){
			$tpl=trim(file_get_contents($template));
			$this->body->load($tpl);
		}
	}
	
	//Output the HTML code.
	public function display(){
		$this->head->addChild($this->charset);
		$this->head->addChild($this->icon);
		$this->head->addChild($this->title);
		$this->root->addChild($this->head);
		$this->root->addChild($this->body);
		echo $this->doctype,$this->root;
	}
	
	//Add javascript code to head.
	public function addScript($js,$is_file=TRUE){
		$js=trim($js);
		if(!empty($js)){
			$script=new HTMLElement('script');
			$script->addAtt("type","text/javascript");
			if($is_file)
				$script->addAtt("src","$js");
			else
				$script->addChild("$js");
			$this->head->addChild($script);
		}
	}
	//Return a <script> element.
	public function getScript($js,$is_file=TRUE){
		$js=trim($js);
		if(!empty($js)){
			$script=new HTMLElement('script');
			$script->addAtt("type","text/javascript");
			if($is_file)
				$script->addAtt("src","$js");
			else
				$script->addChild("$js");
			return $script;
		}
	}
	
	//Add CSS code to head.
	public function addStyle($code,$is_file=TRUE){
		$code=trim($code);
		if(!empty($code)){
			if($is_file){
				$link=new HTMLElement('link');
				$link->addAtt('rel','stylesheet');
				$link->addAtt('type','text/css');
				$link->addAtt('href',"$code");
				$this->head->addChild($link);
			}
			else{
				$style=new HTMLElement('style');
				$style->addAtt('type','text/css');
				$style->addChild($code);
				$this->head->addChild($style);
			}
		}
	}
}
?>